﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using UsersReg;

namespace Sales_and_Services_Monitoring_System
{
    public partial class Form1 : Form
    {
      dBase db = new dBase();
        public Form1()
        {
            InitializeComponent();
            btnlog.Click += new EventHandler(btnLogin_Click);
            this.Load += new EventHandler(Login_Load);
            txtUser.KeyPress += new KeyPressEventHandler(txtUsername_KeyPress);
            txtPass.KeyPress += new KeyPressEventHandler(txtPassword_KeyPress);
            
        }

        public void Search()
        {

            db.query("SELECT * FROM tblUsers WHERE fUsername = '" + txtUser.Text + "' AND fPassword = '" + txtPass.Text + "'");
            if (db.dtl.Rows.Count > 0)
            {
               this.Hide();
               mainfrm frm = new mainfrm();
               string position = db.dtl.Rows[0]["fLevelOfAccess"].ToString();
               /*frm.AccessData(position);*/
               frm.ShowDialog();
               this.Close();
            }
            else
            {

                MessageBox.Show("Incorrect Password or Username", "Confirm", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
                txtUser.Clear();
                txtPass.Clear();
                txtUser.Focus();
                return;
            }
        }


        void txtPassword_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == Convert.ToChar(Keys.Enter))
            {
                Search();
            }
        }

        void txtUsername_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == Convert.ToChar(Keys.Enter))
            {
                Search();
            }
        }

        void Login_Load(object sender, EventArgs e)
        {
            db.connect();
            db.close();
        }

        void btnLogin_Click(object sender, EventArgs e)
        {
            string user = txtUser.Text.ToString();
            string pwd = txtPass.Text.ToString();
            if (user.Trim() != "")
            {
                DataTable dtl = Database.Table("SELECT * FROM tbl1 WHERE User_Name='" + user + "' AND Password='" + pwd + "'");
                if (dtl.Rows.Count > 0)
                {
                    if (comboBox1.Text != "")
                    {
                        string tmp = dtl.Rows[0]["AccessLevel"].ToString();
                        if (dtl.Rows[0]["AccessLevel"].ToString() == "1" && comboBox1.Text == "Admin")
                        {
                            Employee._AccessLevel = Employee.AccessLevel.Admin;
                            Employee.IdNumber = dtl.Rows[0]["ID"].ToString();
                            mainfrm form_Main = new mainfrm();
                            form_Main.Show();
                            this.Hide();
                        }
                        else if (dtl.Rows[0]["AccessLevel"].ToString() == "0" && comboBox1.Text == "User")
                        {
                            Employee._AccessLevel = Employee.AccessLevel.User;
                            Employee.IdNumber = dtl.Rows[0]["ID"].ToString();
                            mainfrm form_Main = new mainfrm();
                            form_Main.Show();
                            this.Hide();
                        }
                    }
                    else
                    {
                        MessageBox.Show("Please select a level of access.");
                    }
                }
                else
                {
                    MessageBox.Show("Invalid Username or Paswword", "ERROR!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            
        }


        
    }
}